package com.lyn.view.button;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.RelativeLayout;
import com.lyn.view.R;

/**
 * @author:longyongning
 * @date:2021/10/2
 * @description: 按钮按下背景替换
 */
public class BackRelativeLayout extends RelativeLayout {

    public BackRelativeLayout(Context context) {
        super(context);
    }

    public BackRelativeLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                this.setBackground(getResources().getDrawable(R.drawable.back_down_bg));
                break;
            case MotionEvent.ACTION_UP:
                this.setBackground(getResources().getDrawable(R.color.colorTransparency));
                break;
            case MotionEvent.ACTION_MOVE:
                this.setBackground(getResources().getDrawable(R.drawable.back_up_bg));
                break;
        }
        return super.onTouchEvent(event);
    }
    // Because we call this from onTouchEvent, this code will be executed for both
    // normal touch events and for when the system calls this using Accessibility
    @Override
    public boolean performClick() {
        super.performClick();
        return true;
    }
}
